package za.co.EYEWTKAH.spring.jms.embedded;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;

public class EmbeddedConnectionFactoryFactory {

	private EmbeddedConnectionFactoryConfiguration configuration;

	private EmbeddedConnectionFactoryType type;

	private EmbeddedConnectionFactoryBuilder builder;

	private EmbeddedConnectionFactory connectionFactory;

	public ConnectionFactory getConnectionFactory() {

		if (connectionFactory == null) {
			connectionFactory = builder.getConnectionFactory();
		}

		return connectionFactory;
	}

	public void initialize() {

		if (type == null) {
			type = EmbeddedConnectionFactoryType.ACTIVEMQ;
		}

		if (configuration == null) {
			configuration = new EmbeddedConnectionFactoryConfiguration(
					"testQueue", "testTopic");
		}

		if (builder == null) {
			builder = EmbeddedConnectionFactoryBuilder.getBuilder(type,
					configuration);
		}

	}

	public void shutDown() {
		if (connectionFactory != null) {
			try {
				connectionFactory.shutDown();
			} catch (JMSException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void setConfiguration(
			EmbeddedConnectionFactoryConfiguration configuration) {
		this.configuration = configuration;
	}

	public void setType(EmbeddedConnectionFactoryType type) {
		this.type = type;
	}

	public void setBuilder(EmbeddedConnectionFactoryBuilder builder) {
		this.builder = builder;
	}

	public void setConnectionFactory(EmbeddedConnectionFactory connectionFactory) {
		this.connectionFactory = connectionFactory;
	}

}
